﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace MarketFunds
{
    public partial class frmNHG : Form
    {
        public OleDbConnection dbconn;
        public frmNHG()
        {
            InitializeComponent();
        }

        private void frmNHG_Load(object sender, EventArgs e)
        {
            dtStartDate.Value = DateTime.Now;
            dtEndDate.Text = dtStartDate.Value.AddDays(Convert.ToInt16(txtDays.Text)).ToShortDateString();
            string strSql = "select  top 200 * from NHG_Main order by hddate desc";
            OleDbCommand cmd = new OleDbCommand(strSql, dbconn);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                int nID = (int)reader["id"];
                DateTime dt = (DateTime)reader["hddate"];
                ListViewItem itm = lvwNHG.Items.Add(dt.ToShortDateString());
                itm.SubItems.Add(reader["amount"].ToString());
                itm.SubItems.Add(reader["Days"].ToString());
                itm.SubItems.Add(reader["Rate"].ToString());
                DateTime dt_expire = (DateTime)reader["Expire_Date"];
                itm.SubItems.Add(dt_expire.ToShortDateString());
                itm.SubItems.Add(StockExcel.CommonUtil.Get当日净投放(dt).ToString("###,###.##"));
                itm.Tag = nID;
            }
            reader.Close();
            cmd.Dispose();
        }

        private void txtDays_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int nDays = Convert.ToInt16(txtDays.Text);
                dtEndDate.Text = dtStartDate.Value.AddDays(nDays).ToShortDateString();
            }
            catch(Exception ex)
            {

            }
        }

        private void dtStartDate_ValueChanged(object sender, EventArgs e)
        {
            int nDays = Convert.ToInt16(txtDays.Text);
            dtEndDate.Text = dtStartDate.Value.AddDays(nDays).ToShortDateString();
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string strSql = string.Format("insert into NHG_Main (hddate,days,amount,rate,expire_date) values (#{0}#,{1},{2},{3},#{4}#)",
                dtStartDate.Value.ToShortDateString(), txtDays.Text, txtAmount.Text, txtRate.Text, dtEndDate.Text);

            OleDbCommand cmd = new OleDbCommand(strSql, dbconn);
            cmd.ExecuteNonQuery();
            cmd.Dispose();

            ListViewItem itm = lvwNHG.Items.Insert(0, dtStartDate.Value.ToShortDateString());
            itm.SubItems.Add(txtAmount.Text);
            itm.SubItems.Add(txtDays.Text);
            itm.SubItems.Add(txtRate.Text);
            itm.SubItems.Add(dtEndDate.Text);
            
            itm.SubItems.Add(StockExcel.CommonUtil.Get当日净投放(dtStartDate.Value).ToString("###,###.##"));

            strSql = "select top 1 id from NHG_Main order by id desc";
            cmd = new OleDbCommand(strSql, dbconn);
            OleDbDataReader reader = cmd.ExecuteReader();
            reader.Read();
            int nID = (int)reader["id"];
            itm.Tag = nID;
            reader.Close();
            cmd.Dispose();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvwNHG.SelectedItems.Count == 0)
                return;
            ListViewItem itm = lvwNHG.SelectedItems[0];
            int nID = (int)itm.Tag;
            string strSql = string.Format("delete from NHG_Main where id={0}", nID);
            OleDbCommand cmd = new OleDbCommand(strSql, dbconn);
            cmd.ExecuteNonQuery();
            lvwNHG.Items.Remove(itm);
            
        }

        private void lvwNHG_DoubleClick(object sender, EventArgs e)
        {
            if (lvwNHG.SelectedItems.Count == 0)
                return;
            ListViewItem itm = lvwNHG.SelectedItems[0];
            int nID = (int)itm.Tag;
            string strSql = "select * from NHG_MAIN where id = " + nID;
            OleDbCommand cmd = new OleDbCommand(strSql, dbconn);
            OleDbDataReader reader = cmd.ExecuteReader();
            reader.Read();
            dtStartDate.Value = (DateTime)reader["hddate"];
            txtDays.Text = reader["Days"].ToString();
            txtAmount.Text = reader["Amount"].ToString();
            txtRate.Text = reader["Rate"].ToString();

        }

        private void btnUPdate_Click(object sender, EventArgs e)
        {
            if (lvwNHG.SelectedItems.Count == 0)
                return;
            ListViewItem itm = lvwNHG.SelectedItems[0];
            int nID = (int)itm.Tag;
            string strSql = string.Format("update NHG_Main set hddate=#{0},Days={1},Amount={2},Rate={3},Expire_date=#{4}# where id={5}",
                 dtStartDate.Value.ToShortDateString(), txtDays.Text, txtAmount.Text, txtRate.Text, dtEndDate.Text, nID);
            OleDbCommand cmd = new OleDbCommand(strSql, dbconn);
            cmd.ExecuteNonQuery();
            itm.SubItems[0].Text = dtStartDate.Value.ToShortDateString();
            itm.SubItems[1].Text = txtAmount.Text;
            itm.SubItems[2].Text = txtDays.Text;
            itm.SubItems[3].Text = txtRate.Text;
            itm.SubItems[4].Text = dtEndDate.Text;
            itm.SubItems[5].Text = StockExcel.CommonUtil.Get当日净投放(dtStartDate.Value).ToString();
        }
    }
}
